package edu.harvard.s3xyback;

import java.io.*;
import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: Ryan
 * Date: 12/8/12
 * Time: 3:35 PM
 * A utility class that sets up the config file for S3xyBack program,
 * using the information provided by user.
 */

public class ConfigClient
{
    private String configPath;

    /**
     * A Constructor for ConfigClient
     * @param inputConfigPath String that contains the desired absolute path name for the configuration file
     */
    public ConfigClient(String inputConfigPath)
    {
        configPath = inputConfigPath;
    }

    // just for testing
    public static void main(String[]args)
    {
        ConfigClient configClient = new ConfigClient("C:\\Users\\ayoung01\\Dropbox\\.properties");
        String[] s = {"testdirectory", "testdirectory2", "testdirectory3"};
        configClient.createConfigFile("awskey", "secret", s, "checkdays", "reportsched", "autorestore");
    }

    /**
     * Takes parameters passed to function and stores them in the proper location to a config file.
     * @param accessKey String containing the AWS access key for Amazon s3
     * @param secretKey String containing the secret key needed for Amazon s3
     * @param directories String array containing a list of directories and files
     * @param checkDays String containing the frequency to check file integrity
     * @param reportSched String containing the frequency for reports to be made
     * @param autoRestore String containing whether or not auto-restore should be used
     */
    public void createConfigFile(String accessKey, String secretKey, String[] directories, String checkDays, String reportSched, String autoRestore)
    {
        // opens a new Properties object
        Properties configProp = new Properties();

        // inputs data passed to method into configProp
        configProp.setProperty("accessKey", accessKey);
        configProp.setProperty("secretKey", secretKey);
        configProp.setProperty("checkDays", checkDays);
        configProp.setProperty("reportSched", reportSched);
        configProp.setProperty("autoRestore", autoRestore);

        String dirs = "";

        // The following generates a comma-separated list of directories
        int length = directories.length;
        for (int i = 0; i < length; i++)
        {
            if (i != (length - 1))
            {
                dirs = dirs + directories[i] + ", ";
            }
            else
            {
                dirs = dirs + directories[i];
            }
        }

        // sets directories field in properties
        configProp.setProperty("directories", dirs);

        //TODO set other properties such as Report schedule

        try
        {
            // comments to be added to the properties file
            String comments = "AWS Access Key ID and Secret Access Key from http://aws.amazon.com/security-credentials\ndirectories: A comma separated list of directories \ncheckDays: How frequently should files be checked?\nreportSched: How frequently should reports be generated?\nautoRestore: Is autoRestore on or off?";

            // stores the data in configProp as a properties file at path specified by configPath
            configProp.store(new FileOutputStream(new File(configPath)), comments);
            System.out.println("New config file successfully generated: " + configPath);
        }
        catch(IOException exp)
        {
            System.out.println("IOException");
        }
    }
}
